package com.comtop.springboot.templet.modules.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.comtop.springboot.templet.common.exception.CustomException;
import com.comtop.springboot.templet.common.validator.Assert;
import com.comtop.springboot.templet.modules.app.mapper.UserMapper;
import com.comtop.springboot.templet.modules.app.entity.UserEntity;
import com.comtop.springboot.templet.modules.app.service.IUserService;
import com.xiaoleilu.hutool.crypto.digest.DigestAlgorithm;
import com.xiaoleilu.hutool.crypto.digest.Digester;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements IUserService {
	/**用户mapper**/
	@Autowired
	private UserMapper userMapper;
	

	@Override
	public void save(String mobile, String password){
		UserEntity user = new UserEntity();
		user.setMobile(mobile);
		user.setUsername(mobile);
		Digester SHA256 = new Digester(DigestAlgorithm.SHA256);
		user.setPassword(SHA256.digestHex(password));
		user.setCreateTime(new Date());
		userMapper.insert(user);
	}
	

	@Override
	public long login(String mobile, String password) {
		UserEntity user = userMapper.queryByMobile(mobile);
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		Digester SHA256 = new Digester(DigestAlgorithm.SHA256);
		if(!user.getPassword().equals(SHA256.digestHex(password))){
			throw new CustomException("手机号或密码错误");
		}
		return user.getUserId();
	}
}
